import { createRouter, createWebHistory } from 'vue-router'
import LayoutView from '../views/layout/LayoutView.vue'
import HomeView from '../views/home/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'LayoutView',
      component: LayoutView,
      // 路由重定向
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'HomeView',
          component: HomeView,
        },{
          path: '/cart',
          name: 'CartView',
          component: () => import('../views/cart/CartView.vue'),
        },
        {
          path: '/order',
          name: 'OrderView',
          component: () => import('../views/order/OrderView.vue') ,
        },
        {
          path: '/my',
          name: 'MyView',
          component: () => import('../views/my/MyView.vue'),
        },

      ]
    },
    // {
    //   path: '/login',
    //   name: 'LoginView',
    //   component: () => import('../views/login/LoginView.vue'),
    // },
    // {
    //   path: '/register',
    //   name: 'RegisterView',
    //   component: () => import('../views/login/RegisterView.vue'),
    // },
    {
      path: '/address',
      name: 'AddressView',
      component: () => import('../views/my/AddressView.vue'),
    },
    {
      path: '/createaddress',
      name: 'CreateAddressView',
      component: () => import('../views/my/CreateAddressView.vue'),
    },
    {
      path: '/shop',
      name: 'ShopView',
      component: () => import('../views/shop/ShopView.vue'),
    },
    {
      path: '/orderconfirmation',
      name: 'OrderConfirmationView',
      component: () => import('../views/orderConfirmation/OrderConfirmationView.vue'),
    }
  ]
})


// 路由守卫
// 前置
// router.beforeEach((to, from, next) => {
//   if(to.path == '/login' || to.path == '/register') {
//     next()
//   }else {
//     // 不是登录或者注册页面
//     if(sessionStorage.getItem('isLogin') == 'true') {
//       // 登录成功
//       next()
//     }else {
//       // 没有登录
//       next({path: '/login'})
//     }
//   }
// })


export default router
